package com.jxs.android.cmcc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;

import com.jxs.android.cmcc.boot.SelfApplication;
import com.jxs.android.cmcc.data.KipInfo;
import com.jxs.android.cmcc.db.KipConfigHolder;
import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.util.StartHelper;
import com.jxs.android.weiget.PopMsg;
import com.jxs.android.weiget.TopBar;

@SuppressLint("HandlerLeak")
public abstract class KipSelectAbstract extends AbstractListActivity {

	protected void refresh(KipConfigHolder.Scope scope) {
		getWebServiceData(SelfApplication.getUserInfo().getTicket(), scope.ordinal() + "", Which.SCOPE, true, new AbstractListActivity.DataInitListeren() {
			@Override
			public void initData(ProgressDialog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		});
	}

	private static final int UPDATE_LIST_UI = 0x403;
	private static final int TO_LOGIN = 0x444;
	private static final int ERROR_MSG = 0x405;
	Handler listHandler = new Handler() {

		@SuppressWarnings("unchecked")
        @Override
		public void handleMessage(Message msg) {
			if (msg.what == UPDATE_LIST_UI) {
				handlerResult((List<KipInfo>) msg.obj);
			} else if (msg.what == ERROR_MSG) {
				PopMsg.show((String) msg.obj);
			} else if (msg.what == TO_LOGIN) {
				PopMsg.show((String) msg.obj);
				StartHelper.startActivity(Login.class);
				finish();
			}
		}

	};

	protected abstract void handlerResult(List<KipInfo> kipInfos);

	@Override
	public String getMethod() {
		return "getKpiTypeList";
	}

	@Override
	protected void paser(String param, SoapObject soapObject) {
		if (null != soapObject) {
			String stauts = soapObject.getPropertySafelyAsString("status");
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getPropertySafelyAsString("message");
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				listHandler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = TO_LOGIN;
				msg.obj = message;
				listHandler.sendMessage(msg);
			} else if ("0".equals(stauts)) {
				SoapObject retMsgObject = (SoapObject) soapObject.getProperty("retMsg");
				List<KipInfo> kipInfoListInfos = new ArrayList<KipInfo>();
				if (null != retMsgObject) {
					for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
						SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);

						KipInfo kipInfo = new KipInfo();
						kipInfo.setKpi_id(taskBeanObject.getPropertySafelyAsString("kpi_id"));
						kipInfo.setKpi_name(taskBeanObject.getPropertySafelyAsString("kpi_name"));
						kipInfo.setKpi_name_1(taskBeanObject.getPropertySafelyAsString("kpi_name_1"));
						kipInfo.setKpi_name_2(taskBeanObject.getPropertySafelyAsString("kpi_name_2"));
						kipInfo.setKpi_name_3(taskBeanObject.getPropertySafelyAsString("kpi_name_3"));
						kipInfoListInfos.add(kipInfo);
					}
				}
				Message msg = new Message();
				msg.what = UPDATE_LIST_UI;
				msg.obj = kipInfoListInfos;
				listHandler.sendMessage(msg);
			}
		}
	}

	protected void KipAddOrDel(final String method, final int position, final KipInfo kipInfo) {
		final ProgressDialog progressDialog = ProgressDialog.show(this, "", "正在加载，请稍候...", true);
		progressDialog.setCancelable(true);
		new Thread(new Runnable() {
			@Override
			public void run() {
				String wsdl_url = SelfApplication.WSDL_URL;// wsdl
				String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
				String methodName = method;// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", SelfApplication.getUserInfo().getTicket());
				request.addProperty("kpiId", kipInfo.getKpi_id());
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						paserKipAddOrDel(position, envelope.getResponse().toString(), progressDialog);
					}
				});
			}
		}).start();
	}

	protected void paserKipAddOrDel(int position, String response, ProgressDialog dialog) {
		Message msg = new Message();
		msg.what = 0x999;
		msg.obj = response;
		msg.arg1 = position;
		addOrDelHandler.sendMessage(msg);
		dialog.dismiss();
	}

	private Handler addOrDelHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0x999) {
				String obj = (String) msg.obj;
				if ("0".equals(obj)) {
					nextStep(msg.arg1);
				} else if ("-1".equals(obj)) {
					PopMsg.show("登录已过期！");
					StartHelper.startActivity(Login.class);
					finish();
				} else if ("-2".equals(obj)) {
					PopMsg.show("服务端异常！");
				}
			}
		}

	};

	protected abstract void nextStep(int position);

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.hindBtn();
		topBar.setTitle(getTopBarTitle());
		initTopBarReturn(topBar);
	}

	protected abstract String getTopBarTitle();

}